package com.example.backend.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.backend.api.common.Result;
import com.example.backend.api.entity.Dept;
import com.example.backend.api.entity.Health;
import com.example.backend.api.mapper.DeptMapper;
import com.example.backend.api.mapper.HealthMapper;
import com.example.backend.api.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

@Service
public class HealthServiceImpl extends ServiceImpl<HealthMapper, Health> implements HealthService {

    @Autowired
    private HealthMapper healthMapper;
    @Autowired
    private DeptMapper deptMapper;
    public Result getAll(Integer userId, String startDate, String endDate, String deptName, String symptom) {
        QueryWrapper<Health> queryWrapper = new QueryWrapper<>();
        if(userId == null && startDate == null && endDate == null && deptName == null && symptom == null) {
            return Result.success(healthMapper.selectList(null));
        }
        if (userId != null ) {
            queryWrapper.eq("user_id", userId);
        }
        if (startDate != null && endDate != null) {
            queryWrapper.between("day", startDate, endDate);
        }
        if (deptName != null) {
            queryWrapper.like("dept_name", deptName);
        }
        if (symptom != null) {
            queryWrapper.like("symptom", symptom);
        }
        return Result.success(healthMapper.selectList(queryWrapper));
    }

    public Result getGroupInfo(String day) {
        QueryWrapper<Health> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("day",day+" 00:00:00",day+" 23:59:59");
        List<Health> healthList =  healthMapper.selectList(queryWrapper);
        return  Result.success(healthList);
    }

    public Result addHealth(Integer userId,String name, Float temperature, String symptom, String date, Integer deptId,String remark) throws ParseException {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", deptId);
        Dept dept = deptMapper.selectList(queryWrapper).get(0);
        String deptName = dept.getName();
        Health health = new Health();
        health.setUserId(userId);
        health.setTemp(temperature);
        health.setSymptom(symptom);
        // convert String to Date
        TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(timeZone);
        health.setDay(sdf.parse(date));
        health.setDeptId(deptId);
        health.setDeptName(deptName);
        health.setPlusNote(remark);
        health.setName(name);
        healthMapper.insert(health);
        return Result.success(health);

    }
}
